package com.zn.config;

import com.zn.realm.UserRealm;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    //Filter工厂，设置对应的过滤条件和跳转条件
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager")DefaultSecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //认证和授权
        Map<String,String> map = new HashMap<>();
        map.put("/a","authc");
        map.put("/b","perms[youke]");
        map.put("/c","roles[admin]");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        //设置登录页面
        shiroFilterFactoryBean.setLoginUrl("/login");
        //设置未授权
        shiroFilterFactoryBean.setUnauthorizedUrl("/meiquanxian");
        return shiroFilterFactoryBean;
    }



    //将定义的用户匹配的角色和角色权限注入到DefaultSecurityManager里面
    @Bean("securityManager")
    public DefaultSecurityManager securityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultSecurityManager defaultSecurityManager = new DefaultWebSecurityManager();
        defaultSecurityManager.setRealm(userRealm);
        return defaultSecurityManager;
    }


    @Bean("userRealm")
    public UserRealm userRealm(){
        return new UserRealm();
    }


}
